package com.windoent.yugioh.base;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.windoent.yugioh.R;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.ExitStackUtils;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.Utils;
import com.windoent.yugioh.widget.dialog.LoadingDialog;

/**
 * author : robert
 * date   : 2021/4/26 14:39
 * desc   :基类
 */
public abstract class BaseActivity extends AppCompatActivity {

    public Context mContext;
    public Gson gson = new Gson();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局
        setContentView(getLayout());
        ExitStackUtils.getInstance().addActivity(this);
        mContext = this;
        //初始化控件
        initView();
        initView(savedInstanceState);
        //设置数据
        initData();
        //设置监听
        initListener();
    }

    public abstract int getLayout();

    /**
     * 初始化布局
     */
    public void initView() {
    }

    public void initView(@Nullable Bundle savedInstanceState) {
    }

    /**
     * 设置数据
     */
    public void initData() {
    }

    /**
     * 设置监听器
     */
    public void initListener() {
    }

    public void immersiveStatusBar() {
        // 1. 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
                getWindow().setNavigationBarColor(Color.parseColor("#0A0E1D"));
                getWindow()
                        .getDecorView()
                        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            } else {
                getWindow()
                        .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }
    }

    public void immersiveStatusBar(View view) {
        // 1. 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
                getWindow().setNavigationBarColor(Color.parseColor("#0A0E1D"));
                getWindow()
                        .getDecorView()
                        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            } else {
                getWindow()
                        .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }
        // 调整状态栏高度
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = getStatusBarHeight();
            if (view == null) {
                return;
            }
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            lp.height = statusBarHeight;
            view.setLayoutParams(lp);
        }
    }

    //状态栏高度
    private int getStatusBarHeight() {
        Resources resources = Utils.getApp().getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    LoadingDialog loadingDialog;

    /**
     * 显示加载框
     */
    public void showLoadingDialog(String title) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mContext, BaseMethod.notNullDefaultValue(title, mContext.getString(R.string.string_loading)));
        }
        loadingDialog.show();
    }

    /**
     * 取消加载框
     */
    public void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    //设置字符
    public void setText(TextView text, String s) {
        text.setText(BaseMethod.notNullDefaultValueNull(s));
    }

    public void setText(EditText text, String s) {
        text.setText(BaseMethod.notNullDefaultValueNull(s));
    }

    protected void uiAction(Runnable runnable) {
        findViewById(android.R.id.content).post(runnable);
    }

    //获取usertoken
    protected String getUserToken() {
        return SharedPreUtil.getString(mContext, Constants.APPTOKEN);
    }

    protected int getAccountId() {
        return SharedPreUtil.getInt(mContext, Constants.USERID, 0);
    }

    //当前位置
    protected String getCurrentLocation() {
        String location = SharedPreUtil.getString(mContext, Constants.LOCATIONCURRENT);
        return BaseMethod.notNullDefaultValue(location, getString(R.string.string_location_defeat));
    }    //当前位置code

    //当前位置code
    protected String getCurrentLocationCode() {
        return BaseMethod.notNullDefaultValueNull(SharedPreUtil.getString(mContext, Constants.LOCATIONCURRENTCITYCODE));
    }


    //是否登录
    public boolean isLogIn() {
        return BaseMethod.notNull(getUserToken());
    }
}
